package ex;

public class ExceptionDemo6 {

    public static void main(String[] args) {
        Teacher teacher = new Teacher("毕老师");
        try {
            teacher.prelect();
        } catch (NoPlanException e) { // NoPlanException e = new NoPlanException(e.getMessage()+",课时停止");
            //throw new RuntimeException(e);
            System.out.println(e);
            System.out.println("换老师");
        }
    }

}
class Teacher {
    private String name;
    private NoteBook noteBook;

    public Teacher(String name) {
        this.name = name;
        noteBook = new NoteBook();
    }

    public void prelect() throws NoPlanException {
        try {
            noteBook.run();
        } catch (LanPingException e) {
            System.out.println(e);
            //e.printStackTrace();
            noteBook.reset();
        } catch (MaoYanException e){
            System.out.println(e);
            test();
            throw new NoPlanException(e.getMessage()+",课时停止"); // 异常转换
        }
        System.out.println(name + "...讲课");
        System.out.println("hello world");
    }

    /**
     * 电脑冒烟之后，老师留练习题给学生
     */
    private void test() {
        System.out.println("做练习");
    }
}

/**
 * 笔记本
 *  功能：运行，重启
 */
class NoteBook {
    private int state = 2;

    public void run() throws LanPingException,MaoYanException{
        System.out.println("笔记本运行...");
        if(state == 1){
            throw new LanPingException("电脑蓝屏了");
        }
        if(state == 2){
            throw new MaoYanException("电脑冒烟了");
        }
    }

    public void reset(){
        state = 0;
        System.out.println("电脑重启");
    }
}

/**
 * 电脑蓝屏异常
 */
class LanPingException extends Exception{
    public LanPingException() {
        super();
    }
    public LanPingException(String message) {
        super(message);
    }
}

/**
 * 电脑冒烟异常
 */
class MaoYanException extends Exception{
    public MaoYanException() {
        super();
    }

    public MaoYanException(String message) {
        super(message);
    }
}

class NoPlanException extends Exception{
    public NoPlanException() {
        super();
    }

    public NoPlanException(String message) {
        super(message);
    }
}
